﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace translator
{
    /// <summary>
    /// OptionsWindow.xaml 的交互逻辑
    /// </summary>
    public partial class OptionsWindow : Window
    {
        public OptionsWindowModel model;
        public MainWindow mainWindow;
        public OptionsWindow(MainWindow mainWindow)
        {
            this.mainWindow = mainWindow;
            this.model = new OptionsWindowModel();
            InitializeComponent();
            ReadAPI();
            this.DataContext = model;
            this.ApiGrid.DataContext = model;
            this.UsageAPIGrid.DataContext = model;
            mainWindow.Focusable = false;
            this.Visibility = Visibility.Visible;
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.ResizeMode = ResizeMode.NoResize;

            this.Focus();

            this.Dictionarypath.Text = Properties.Settings.Default.DictionaryPath;
        }

        public void ReadAPI()
        {
            List<KeyInfo> lst = new List<KeyInfo>(50);
            foreach (var str in Const.ReadKeys("TranslaterAPI"))
            {
                lst.Add(new KeyInfo(str, Const.ReadIniData("TranslaterAPI", str, "")));
            }
            this.model.ApiLst = new ObservableCollection<KeyInfo>(lst);
            lst.Clear();
            foreach (var str in Const.ReadKeys("APIusage"))
            {
                lst.Add(new KeyInfo(str, Const.ReadIniData("APIusage", str, "")));
            }
            this.model.UsageAPILst = new ObservableCollection<KeyInfo>(lst);
        }

        private void ApiGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            KeyInfo htmp = (e.Row.Item as KeyInfo);
            htmp.Value = (e.EditingElement as TextBox).Text;
            Const.WriteIniData("TranslaterAPI", htmp.Key, htmp.Value);
        }

        private void UsageAPIGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            KeyInfo htmp = (e.Row.Item as KeyInfo);
            htmp.Value = (e.EditingElement as TextBox).Text;
            Const.WriteIniData("APIusage", htmp.Key, htmp.Value);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            mainWindow.Focusable = true;
            mainWindow.Focus();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = System.Environment.CurrentDirectory;      //Application.StartupPath 
            //openFileDialog.FileName = "翻译中间文件 (*.db)|*.db|All files (*.*)|*.*";
            openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = false;
            if (openFileDialog.ShowDialog() == true)
            {
                mainWindow.Log("Selected file path: " + openFileDialog.FileName + " ...");
                if (!File.Exists(openFileDialog.FileName)) { mainWindow.Log("File Not Exists!"); return; }
                Properties.Settings.Default.DictionaryPath = openFileDialog.FileName;
                this.Dictionarypath.Text = Properties.Settings.Default.DictionaryPath;
            }
        }


        private void Dictionarypath_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            if (!File.Exists(Dictionarypath.Text)) { mainWindow.Log("File Not Exists!"); return; }
            Properties.Settings.Default.DictionaryPath = Dictionarypath.Text;
            CommandManager.InvalidateRequerySuggested();
        }

        private void Dictionarypath_TextChanged(object sender, TextChangedEventArgs e)
        {
            Dictionarypath.ScrollToEnd();
        }
    }
    public class OptionsWindowModel : INotifyPropertyChanged
    {
        private ObservableCollection<KeyInfo> apiLst;
        public ObservableCollection<KeyInfo> ApiLst
        { get { return apiLst; } set { apiLst = value; if (PropertyChanged != null) PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ApiLst")); } }
        private ObservableCollection<KeyInfo> usageAPILst;
        public ObservableCollection<KeyInfo> UsageAPILst
        {
            get { return usageAPILst; }
            set { usageAPILst = value; if (PropertyChanged != null) PropertyChanged.Invoke(this, new PropertyChangedEventArgs("UsageAPILst")); }
        }
        public OptionsWindowModel()
        {

        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
    public class KeyInfo
    {
        public string Key { get; set; }
        public string Value { get; set; }
        public KeyInfo()
        {
           
        }
        public KeyInfo(string Key,string Value)
        {
            this.Key = Key;
            this.Value = Value;
        }
    }
}
